<template>
<!-- 评论列表 -->
  <van-list
    v-model="loading"
    :finished="finished"
    finished-text="没有更多了"
    @load="onLoad"
  :immediate-check="false"
  >
  <!-- 文章评论项 -->
   <CommentItem  v-for="(value,index) in list" :key="index" 
   :comment="value"
   @reply-click="$emit('reply-click', $event)"
    />
    <!-- <van-cell v-for="(value,index) in list" :key="index"
   :title="value.content"
    /> -->
   
  </van-list>
</template>

<script>

import{getComments}from'@/api/comment'

// 导入列表
import CommentItem from './comment-item'
export default {
  name: "CommentList",
  components: {

      CommentItem
  },
  // 传入的id
  props: {
    source: {
      type: [Number, String, Object],
      required: true
    },
    list: {
      type:Array,
      default:()=>[],
    },
    type:{
      type:String,
// 验证
       validator (value) {
        return ['a', 'c'].includes(value)
      },
      default:'a'
    }
  },
  data() {
    return {
      
      loading: false,
      finished: false,
       offset: null, // 获取下一页数据的标记 (分页，从哪开始取这页数据)
      limit: 10,// (分页，每页多少条)
      error: false
    };
  },
  computed: {},
  watch: {},
  created() {
this.onLoad()

  },
  mounted() {},
  methods: {
    async onLoad() {

    try {
        const {data} = await getComments({
            type:this.type, // a是获取评论
            source:this.source.toString(),
            offset:this.offset, // 获取评论数据的偏移量，
            // 值为评论id，表示从此id的数据向后取，不传表示从第一页开始读取数据
            limit:this.limit

        })
        this.list.push(...data.data.results)
        // console.log(this.list);
        this.loading = false
       if (data.data.results.length>0) {
           
            this.offset = data.data.last_id
       }else{this.finished =true}
       // 获取评论数量传递给父组件
       this.$emit('onload-success',data.data)
    } catch (error) {
        this.error = true;
        this.loading = false
    }
     
      
  }
  }
};
</script>

<style scoped lang="less">
</style>